<?php
/*
 * Created on 05.03.2008
 *
 * filename: system.php
 */
  class Controller_System extends Controller_Base_Admin {
  	
  	function index($args) {
		$template = new Template('admin/index.tmpl');

		$template->setArgs(array(
			menu	=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
			tabs	=> $this->tabs->get('help'),
			_user	=> $this->admin,
		));

		return $template;

  	}	
  
	function access($args) {
					
		$selected_id = $this->admin->find(array(login => $args[0]))->id;
		
		if(isset($_POST['save_rules'])) {
			error_log("SAVING RULES $_POST[user_rules] user: ".$_POST[user_name]);
		
			$rights = preg_split ("/,/",$_POST[user_rules]);  
			
			if($_POST[user_rules] == $this->admin->id) {
				$rights[] = $this->id;
			}

/*
//			// hack to not disalow self-access
//			if($this->registry[logged][user_id] == $_POST[user_rules]){
//				$out[] = array(
//					user_id	=> $this->admin->id,
//					id		=> $this->id
//				);
//			
//				$out[] = array(
//					user_id	=> $this->admin->id,
//					id		=> 1
//				);
//			}
//
//			foreach($rights as $r) {
//				$out[] = array(
//					user_id	=> $selected_id,
//					id		=> $r
//				);
//			}
//print "<pre>";
//print_r($out);
//print "</pre>";			
	*/		
			$this->admin->saveRules($rights,$selected_id);

			return new Redirect("/admin/system/access/$_POST[user_name]/");
		}

		$template = new Template('admin/index.tmpl');
		
		$full_menu = $this->menu->getHierarchicalMenu(array(uid 	=> $selected_id, show	=> 'all'));

		$json = new Services_JSON();

		$template->setArgs(
			array(
				'menu'		 	=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
				'tabs'			=> $this->tabs->get('access'),
				'json'			=> $json->encode($full_menu),
				'users'			=> $this->admin->findAll(),
				'user_selected'	=> $args[0], //$this->admin->name,//$user->getUserName($user_id)
				'_user'			=> $this->admin	 
			)
		);

		return $template;
 	}
 	
 	function sort($args) {
				
		$template = new Template('admin/index.tmpl');
		
		$template->setArgs(array(
			menu	=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
			tabs	=> $this->tabs->get('sort'),
			'_user'	=> $this->admin,
		)); 

		return $template;
 	}  	
 	
	function users($args) {
		
		$template = new Template('admin/index.tmpl');
		
//print_r($_POST);		
		
		if($_POST['action'] === 'new') {
			
			try{	
				
				$_POST[real_name] = $_POST[new_user_name];
				$result = $this->admin->add($_POST);
				
				// TODO hack - need to refactor
				if($result)
					$this->admin->confirmEmail($result);
					
			} catch (UserException $e) {
				error_log("USER ADD ERROR: ".$e->getMessage());
				$template->setArg('registration_error', $e->getMessage());
			}
			
			if(!empty($result))
				return new Redirect('/admin/system/users/'.$_POST['new_user_name']);

		} else if ($_POST['action'] == 'change_password') {

			try{
				$this->admin->changePassword($_POST['password1'],$_POST['password1'],$this->admin->find(array(login => $_POST[user]))->id);
			} catch (UserException $e) {
				
				error_log(" cought exception:  ".$e->getMessage());
				$error = $e->getMessage();
			}
			
			if (empty($error))
				return new Redirect('/admin/system/users/'.$_POST['new_user_name']);

		} else if ($_POST['action'] == 'delete') {
			$this->admin->delete($_POST['user']);

			return new Redirect('/admin/system/users/');
		}

		$template->setArg('change_error', $error);

		$template->setArgs(
			array(
				menu			=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
				users			=> $this->admin->findAll(),
				user_selected 	=> $args[0] ? $args[0] : $this->admin->login, 
				tabs			=> $this->tabs->get('users'),
				'_user'			=> $this->admin,
			)
		);

		return $template;
	
	} 	
	
	function setup ($args) {

		$site = new Model_Site();
			
		if($_POST['save'] === '1') {

	 		$sigs = array(
	 			'header_text'	=> array('type' => 'string','function'=>array('stripslashes')),
	 			'footer_text'	=> array('type' => 'string','function'=>array('stripslashes')),
	 			'title'			=> array('type' => 'string','function'=>array('stripslashes')),
	 			'info_block'	=> array('type' => 'string','function'=>array('stripslashes')),
	 			'visibility'	=> array('type' => 'int'),
				'access'		=> array('type' => 'int'),
				'save'			=> array('type' => 'int',)
			);			
		
			$_POST['header_text']	= strip_tags($_POST['header_text'],'<br />');
			$_POST['footer_text']	= strip_tags($_POST['footer_text'],'<br />');
			$_POST['title'] 		= strip_tags($_POST['title']);
				
			sanitize_vars($_POST,$sigs);

			$site->saveAll(array(
				$_POST['header_text'],
				$_POST['footer_text'],
				$_POST['visibility'] , 
				$_POST['access'],
				$_POST['title'],
				$_POST['info_block']
			));
			
			return new Redirect('/admin/system/setup/');
		}

		if($_POST['save'] === '2') {
			$site->saveIndex($_POST);

			return new Redirect('/admin/system/setup/#setup-index');		
		}

		$template = new Template('admin/index.tmpl');

		$template->setArgs(
			array(
				menu 	=> $this->menu->getHierarchicalMenu(array(uid => $this->admin->id)),
				tabs 	=> $this->tabs->get('setup'),
				site	=> $site,
				_user	=> $this->admin,
			)
		);
		
		return $template;
	}
    
    function feeds($args) {
//print "<pre>";
//print_r($_POST);
//print "</pre>";    
        if($_POST[action] === 'add')       
            $this->admin->addFeed($_POST['object']);
        else if ($_POST[action] === 'delete') 
            $this->admin->deleteFeed($_POST['object']);
        else
            throw new Exception('unknown feed action');
    
        return new Redirect($_POST[back]);
    }    
 }
 
?>
